<?php


namespace App\Traits;


use App\Jobs\SendMiniTemplateMsg;
use App\Models\OauthBind;
use App\Models\TemplateMessage;
use App\Models\Warehouse;
use App\Models\WarehouseHasMember;
use App\Models\WarehouseRecord;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

trait SendTemplateMsg
{
    /**
     * @param $openid oroCI5GvA69Wv6mwRRXMSSRPoHGw
     * 'data'=>[
    'thing1'=>['value'=>'入库申请'], //审核类型
    'name3'=>['value'=>'张三'], //申请人
    'thing10'=>['value'=>'商品1'], //备注
    'phrase8'=>['value'=>'待审核'], //审核结果
    'time5'=>['value'=>'2020-05-25'], //申请时间
    ]
     * 商品名称
    {{thing2.DATA}}

    订单金额
    {{amount3.DATA}}

    下单时间
    {{date4.DATA}}

    收货人
    {{name5.DATA}}

    收货地址
    {{thing6.DATA}}
     */
    public function sendTemplate($type,$orders){
        Log::info("time:".date("Y-m-d H:i:s",time()));
//        $openid = 'oRo2F5OME0Lru-OUoUJME13MCyIE';
        $openid = 'oRo2F5MBb1D65nQm7UbZV7-izT8s';
        $token = $this->getAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$token;
        if($type == 'new') {
            $content = [
                'touser' => $openid,
                'template_id' => 'qqMNeioEFUTZGZ9Z9P2wJHkGuyIqUqvR5R6WHaBMyL4',
                'page' => '/pages/index/index',
                'miniprogram_state' => 'formal',
                'lang' => 'zh_CN',
                'data' => [
                    'thing2' => ['value' => $orders['name']], //商品名称
                    'amount3' => ['value' => $orders['money']], //订单金额
                    'date4' => ['value' => $orders['time']], //下单时间
                    'name5' => ['value' => $orders['user']], //收货人
                    'thing6' => ['value' => $orders['address']], //收货地址
                ]
            ];
        } else {
            $content = [
                'touser' => $openid,
                'template_id' => 'mnBFXQxJFiSvdsYSrNgEXa97JqON1TFnA7NSZk38JnI',
                'page' => '/pages/index/index',
                'miniprogram_state' => 'formal',
                'lang' => 'zh_CN',
                'data' => [
                    'character_string9' => ['value' => $orders['order_no']], //商品名称
                    'thing2' => ['value' => $orders['name']], //商品名称
                    'amount3' => ['value' => $orders['money']], //退款金额
                    'date6' => ['value' => $orders['time']], //退款时间
                    'thing7' => ['value' => $orders['way']], //退款方式
                ]
            ];
        }
        $res = $this->curlHtml($url,json_encode($content));
        return $res;
    }



    /**
     * @return mixed
     * token 获取
     */
    public function getAccessToken(){
        if(Redis::exists("miniapp:accesstoken")){
            $access_token = Redis::get("miniapp:accesstoken");
        }else{
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx6ae7645c51ecfdaa&secret=8bbc0181991175add6c051ca5a7ca657";
            $res = json_decode(file_get_contents($url),true);
            if(!key_exists('errcode',$res)){
                $access_token = $res['access_token'];
                Redis::setex('miniapp:accesstoken',5000,$access_token);
            }else{
                return $this->getAccessToken();
            }
        }
        return $access_token;
    }

    /**
     * @param $url
     * @param string $data
     * @return mixed
     */
    function curlHtml($url, $data = ''){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if(!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }





}